package reduce.fz.com.reduce.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbRecycleScroollListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener3;
import reduce.fz.com.reduce.activity.FindTeamActivity;
import reduce.fz.com.reduce.adapter.FindAdapter;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.event.DoorFragmentShowEvent;
import reduce.fz.com.reduce.event.FragmentEnum;
import reduce.fz.com.reduce.fragment.base.BaseFragment;
import reduce.fz.com.reduce.service.FindService;
import reduce.fz.com.reduce.util.JsonUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;


public class FindFragment extends BaseFragment {

    private SwipeRefreshLayout swipe_refresh_widget;
    private RecyclerView findCard_list;
    private GridLayoutManager mLayoutManager;
    private Pagination pagination = new Pagination();
    private List<TeamCategory> categories;
    private FindAdapter adapter;

    private FindService findService = ServiceBuilder.builderFindService(this.getActivity());

    public static FindFragment create() {
        return new FindFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        initWidget(view);
        return view;
    }

    public void initWidget(View view) {
        swipe_refresh_widget = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        findCard_list = (RecyclerView) view.findViewById(R.id.findCard_list);
        findCard_list.setHasFixedSize(true);
        mLayoutManager = new GridLayoutManager(view.getContext(), 3);
        findCard_list.setLayoutManager(mLayoutManager);
        findCard_list.setItemAnimator(new DefaultItemAnimator());
        adapter = new FindAdapter(view.getContext(), test(), new FindAdapter.FindListener() {
            @Override
            public void onClickFolder(TeamCategory category) {
                fowwardFindTeams(category.getCategoryCode());

            }
        }
        );
        findCard_list.setAdapter(adapter);
        findCard_list.setOnScrollListener(new AbRecycleScroollListener(mLayoutManager) {
                                              @Override
                                              public void onLoadMore() {
                                                  adapter.addFolderList(test());

                                                  /*HashMap map = new HashMap();
                                                  findService.LoadFindTopic(map, new AbResponseListener<List<TeamCategory>>(new TypeReference<Result<List<TeamCategory>>>() {
                                                  }) {
                                                      @Override
                                                      public void responseInfo(Result<List<TeamCategory>> data) {
                                                          adapter.addFolderList(data.getData());
                                                      }
                                                  });*/
                                              }
                                          }
        );
        swipe_refresh_widget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pagination.reset();
                adapter.addFolderList(test());
                swipe_refresh_widget.setRefreshing(false);
                /*findService.LoadFindTopic(null, new AbResponseListener<List<TeamCategory>>(new TypeReference<Result<List<TeamCategory>>>() {
                }) {
                    @Override
                    public void responseInfo(Result<List<TeamCategory>> data) {
                        adapter.addFolderList(data.getData());
                    }
                });*/
            }
        });
    }

    public void fowwardFindTeams(String teamCode) {
        EventBus.getDefault().post(new DoorFragmentShowEvent<HashMap>(FragmentEnum.FINDTEAMFRAGMENT, null));
    }


    public List<TeamCategory> test() {
        List<TeamCategory> teamCategoryList = new ArrayList<TeamCategory>();
        for (int i = 0; i < 18; i++) {
            teamCategoryList.add(TeamCategory.create());
        }
        return teamCategoryList;
    }

}
